import axios from 'axios';
import crypto from 'crypto';
import { NextResponse } from 'next/server'

export async function POST(request: Request) {
  let apiKey = process.env.BINANCE_API_KEY!
 // console.log(process.env.BINANCE_API_KEY!, "process.env.BINANCE_API_KEY!");

  let apiSecret = process.env.BINANCE_API_SECRET!
  const timestamp = Date.now();
  const queryString = `timestamp=${timestamp}`;
  const signature = crypto
    .createHmac('sha256', apiSecret)
    .update(queryString)
    .digest('hex');

  const url = `https://fapi.binance.com/fapi/v3/positionRisk?${queryString}&signature=${signature}`;
  const res = await axios.get(url, {
    headers: { 'X-MBX-APIKEY': apiKey }
  });
  if(res.data.length===0){
    return NextResponse.json({ success: true, data: 'No position data available' })
  }
  return NextResponse.json({ success: true, data: res.data })

  
}
